Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

269
Visualizações
React Hook "useCustomHook" is called conditionally. React Hooks must be called in the exact same order in every component render

I got error when i want to set if in customHook Code

  if (dataValid) {
    const contactForm = useDataForm(onSubmit, modelToForm(dataValid));
  }

After this i got error ->

React Hook "useDataForm" is called conditionally. React Hooks must be called in the exact same order in every component render 

Problem is if statment when call this work good ( without if ) ->

 const contactForm = useDataForm(onSubmit, modelToForm(dataValid));

I wan't to load useDataForm ( custom hook ) if dataValid not valid and filled.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

We can’t call Hooks inside of conditionals, loops, or nested functions in order to ensure that Hooks are called in the same order each time a component renders. The order is important for how React associates Hook calls with components.

Resource: https://www.benmvp.com/blog/conditional-react-hooks/#:~:text=We%20can%27t%20call%20Hooks,associates%20Hook%20calls%20with%20components.

You can check this resource maybe it can be helpful

about 4 years ago · Juan Pablo Isaza Relatório

0

As per React documentation, you cannot call hooks conditionally.

Here is explained in depth why.

When the need to call a hook conditionally arises, you could opt for two soutions :

  1. Either you call useDataForm and then you use contactForm only if dataValid is true
const contactForm = useDataForm(onSubmit, modelToForm(dataValid));
if (dataValid) {
    // do what you need to do with dataValid
}
// or 
return <Child data={dataValid ? contactForm : otherData} />
  1. Either you move the hook in a separate component and render said component only if dataValid is true
  2. Either, depending on the hook, you can pass the arguments conditionally. e.g. in your exemple:
const contactForm = useDataForm(onSubmit, dataValid   ? modelToForm(dataValid) : fallbackArg);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda